<template>
	<div class="bg">
		<el-card class="form-card">
			<div slot="header" class="clearfix">
				<span>登录</span>
			</div>
			<el-form label-position="right" label-width="80px" :model="form" :rules="rules" ref='form'>
				<el-form-item label="账号" prop="username">
					<el-input v-model="form.username"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="form.password" type="password"></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="checkpass">
					<el-input v-model="form.checkpass" type="password"></el-input>
				</el-form-item>
				<el-form-item label="姓名" prop="fullname">
					<el-input v-model="form.fullname"></el-input>
				</el-form-item>
				<el-form-item label="性别" prop='sex'>
					<el-radio-group v-model="form.sex">
						<el-radio label="男"></el-radio>
						<el-radio label="女"></el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="手机" prop="tel">
					<el-input v-model="form.tel"></el-input>
				</el-form-item>
				<el-form-item  prop='agree'>
					<el-checkbox v-model="form.agree">同意本站用户协议</el-checkbox>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click='handleregister'>注册</el-button>
				</el-form-item>
			</el-form>
			<div class="action-box">
				<el-link type='info' href='#/login'>登录账号</el-link>
				<el-link type='info' href='#/'>忘记密码?</el-link>
			</div>
		</el-card>

	</div>
</template>

<script>
	import { Admin } from '@/api';
	export default {
		data() {
			 var validatePass2 = (rule, value, callback) => {
			      if (value === '') {
			        callback(new Error('请再次输入密码'))
			      } else if (value !== this.form.password) {
			        callback(new Error('两次输入密码不一致!'))
			      } else {
			        callback()
			      }
			    }
			return {
				form: {
					username: '',
					password: '',
					checkpass:'',
					fullname: '',
					sex: '',
					tel: '',
					agree:'',
				},
				rules: {
					username: [
						{ type: 'string', required: true, message: '请输入账户名称', trigger: 'blur' },
						{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
					],
					password: [
						{ required: true, message: '请输入密码', trigger: 'blur' },
						{ pattern: /^\d{3,15}$/, message: '长度在 3 到 15 个数字', trigger: 'blur' },
					],
					checkpass:[
						{ required: true, validator: validatePass2, trigger: 'blur' },
					],
					fullname: [
						{ type: 'string', required: true, message: '请输入姓名', trigger: 'blur' },
						{ min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' },
					],
					sex: [
						{ required: true, message: '请选择性别', trigger: 'blur' },

					],
					tel: [
						{ required: true, message: '请输入手机号码', trigger: 'blur' },
						{ pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
							message: '请正确输入手机号', trigger: 'blur' },
					],
					agree:[
					{ required: true, message: '是否同意本协议', trigger: 'blur' },	
					]
				}
			};
		},
		methods: {
			handleregister() {
				//表单验证
				this.$refs.form.validate(async (valid) => {
					if (valid) {
						//发送ajax
				let {status,msg,data} = await Admin.register({...this.form});
					if(status){
						//缓存数据
						sessionStorage.id = data.id;
						sessionStorage.role = data.role;
						sessionStorage.token = data.token;
						//提示成功
						this.$message.success(msg)
						//跳转页面
					this.$router.replace('/admin')
					} else {
						//提示失败
						this.$message.error(msg)
					}
					} 
				});
			},
		},
	}
</script>

<style lang="less" scoped>
	.bg {
		position: relative;
		background-image: url(~@/assets/img/register/bg.jpg);
		background-repeat: no-repeat;
		width: 100vw;
		height: 100vh;
		background-size: cover;

		.form-card {
			position: absolute;
			top: 50%;
			transform: translateY(-50%);
			right: 100px;
			width: 360px;
		}

		.action-box {
			display: flex;
			justify-content: space-between;
		}

	}
</style>
